package com.cskaoyan.service.impl;

import com.cskaoyan.mapper.CollectMapper;
import com.cskaoyan.mapper.GoodsMapper;
import com.cskaoyan.mapper.UserMapper;
import com.cskaoyan.model.Collect;
import com.cskaoyan.model.Comment;
import com.cskaoyan.model.Goods;
import com.cskaoyan.model.bo.BaseParamBO;
import com.cskaoyan.model.vo.CollectorDataVO;
import com.cskaoyan.model.vo.CollectorVO;
import com.cskaoyan.model.vo.ExceptionVO;
import com.cskaoyan.model.vo.GoodsDataVO;
import com.cskaoyan.service.CollectorService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: 微信小程序
 * @description:
 * @author: Wang
 * @create: 2022-05-08 22:52
 **/

@Service
public class CollectorServiceImpl implements CollectorService {

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    UserMapper userMapper;
    /*
     * description 获取用户信息
     * @author Wangtianhao
     * @date 2022-05-10 14:21
     */
    public Object getUserInfo (){
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            //在已经认证成功的情况下，可以获得用户信息
            // 获得的用户信息的来源 → 来源realm的doGetAuthenticationInfo方法的返回值的第一个参数
            Object primaryPrincipal = subject.getPrincipals().getPrimaryPrincipal();
            return primaryPrincipal;
          }
            return ExceptionVO.error(700, "账号未登录");
    }

    /*
     * description 获取收藏夹列表
     * @author Wangtianhao
     * @date 2022-05-09 20:09
     */
    @Override
    public CollectorDataVO queryCollector(Integer userId, BaseParamBO param) {
        String username = (String)getUserInfo();
        Integer userIds = userMapper.selectUserId(username);

        Integer page = param.getPage();//页码
        Integer limit = param.getLimit();//数据量
        String sort = param.getSort();//排序列
        String order = param.getOrder();//desc\asc
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        //执行查询过程中拼接分页信息
        List<CollectorVO> list = collectMapper.queryList(userIds,param);

        //total、pages、list
        //通过PageHelper帮我们来获得一些和分页相关的信息PageInfo
        //使用其有参构造方法 → 形参写来源于数据库的list
        PageInfo<CollectorVO> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        return CollectorDataVO.data(total,pages,limit,page,list);
    }

    /*
     * description 商品收藏的添加和删除
     * @author Wangtianhao
     * @date 2022-05-09 20:09
     */
    @Override
    public void addOrDelete(Integer valueId, Integer type) {
        String username = (String)getUserInfo();
        Integer userIds = userMapper.selectUserId(username);

        if (collectMapper.selectAddOrDelete(valueId)== 0) {
            collectMapper.insertCollector(valueId ,userIds);
        } else {
            Integer deleted = collectMapper.selectCollector(valueId);
            collectMapper.addOrDelete(deleted, valueId);
        }
    }
}
